/**********************************************************************************
* CLASS
*		BookToolbar
*
* DESCRIPTION
*		Generate toolbar by create genesis and add table of image inside.
*
* REVISION HISTORY
*		Revision 1.0 2008/08/02 Atthaboon S.
*		First Release
*
**********************************************************************************/
var BookToolbar = Class.create({
	
	/****************************************************
	* FUNCTION
	*		initialize
	*
	* DESCRIPTION
	* 		initialize the Toolbar object.
	*
	* ARGUMENTS
	*		imagemin_width	:	imagemin_width   (default : 75)
	* 		imagemin_height	:	imagemin_height  (default : 75)
	*		imagemax_width	:	imagemax_width   (default : 150)
	* 		imagemax_height	:	imagemax_height  (default : 150)
	*		table_width		:	table_width            (default : 150)
	* 		talbe_height	:	talbe_height          (default : 150)	
	*
	****************************************************/
	initialize: function(imagemin_width,imagemin_height,imagemax_width,imagemax_height,table_width,talbe_height)
  	{
  	    this.image_default_width  = imagemin_width;
		this.image_default_height = imagemin_height;
		this.image_max_width      = imagemax_width;
		this.image_max_height	  = imagemax_height;
		
		this.toolbarImg_obj 	  = new Array();
		this.toolbarTD_obj		  = new Array();
		
		this.table      = new Element('table',{'id':'toolbareye','border':'0','style':'position:absolute;width:'+table_width+'px;height:'+talbe_height+'px;left:0px;top:0px;'});
  	},
	
	/****************************************************
	* FUNCTION 
	*		getDomType
	*
	* DESCRIPTION
	* 		Use by Genesis for get Genesis dom Component Type.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomType : function()
	{
		return "BookToolbar";
	},
  	
  	/****************************************************
	* FUNCTION 
	*		getDomObj
	*
	* DESCRIPTION
	* 		Use by Genesis for get Genesis dom Component.
	*
	* ARGUMENTS
	*
	****************************************************/
	getDomObj : function()
	{
		return this.table;
	},  	
	
	/****************************************************
	* FUNCTION 
	*		getImgObj
	*
	* DESCRIPTION
	* 		Get toolbar image object.
	*
	* ARGUMENTS
	*
	****************************************************/
	getImgObj : function()
	{
		return this.toolbarImg_obj;
	},  

	/****************************************************
	* FUNCTION 
	*		getTDObj
	*
	* DESCRIPTION
	* 		Get toolbar td object.
	*
	* ARGUMENTS
	*
	****************************************************/
	getTDObj : function()
	{
		return this.toolbarTD_obj;
	},  	
	
  	/****************************************************
	* FUNCTION 
	*		addImage
	*
	* DESCRIPTION
	* 		Add Image object to toolbar.
	*
	* ARGUMENTS
	*		src		:	BookImage src.
	*
	****************************************************/
	addImage : function(src,width,height)
	{
		var imageObj = new BookImage(src,100,5);
		imageObj.setSize(width,height);
		imageObj.getDomObj().style.position = ''; // remove position absolute.
		
		var td	  = new Element('td',{'border':'0','align':'center','valign':'top','height':'150'});
		td.appendChild(imageObj.getDomObj());
		
		this.toolbarImg_obj[this.toolbarImg_obj.length] = imageObj;
		this.toolbarTD_obj[this.toolbarTD_obj.length]   = td;
	},
	
  	/****************************************************
	* FUNCTION 
	*		setImageMaxWidth
	*
	* DESCRIPTION
	* 		Set Image max width that can grow.
	*
	* ARGUMENTS
	*		width		:	max width of image in pixel.
	*
	****************************************************/
	setImageMaxWidth : function(width)
	{
		this.image_max_width      = width;
	},
	
  	/****************************************************
	* FUNCTION 
	*		resizeToDefault
	*
	* DESCRIPTION
	* 		Resize the toolbar icon to the original size
	*
	* ARGUMENTS
	*
	*
	****************************************************/
	resizeToDefault : function()
	{
		// resize to 75 * 75 (normal size)
		for(var i =0;i<this.toolbarImg_obj.size();i++)
		{
			this.toolbarImg_obj[i].setSize(this.image_default_width,this.image_default_height);
		}
	},
	
  	/****************************************************
	* FUNCTION 
	*		genToolbar
	*
	* DESCRIPTION
	* 		Generate the toolbar object dom after set image already.
	*
	* ARGUMENTS
	*
	*
	****************************************************/
	genToolbar : function()
	{
        var tableBody  = new Element('tbody',{});
        var tr	  	   = new Element('tr',{});
        tableBody.appendChild(tr);
        this.table.appendChild(tableBody);
        
        var tds	  = new Element('td',{'border':'0','align':'center','valign':'top','height':'150','width':'100'});
        tr.appendChild(tds);
        
        var toolbarTD_obj 	 = this.toolbarTD_obj;
        var toolbarImg_obj   = this.toolbarImg_obj;
        var default_width    = this.image_default_width;
		var default_height   = this.image_default_height;
		var max_width		 = this.image_max_width;
		var max_height 	     = this.image_max_height;
        
        for(var i=0;i<this.toolbarTD_obj.size();i++)
        {
        	tr.appendChild(this.toolbarTD_obj[i]);
        	this.toolbarTD_obj[i].onmousemove = function(e) {
				e = e ? e : window.event;
				var mouse_x = e.clientX;
				var mouse_y = e.clientY;
				for(var i =0;i<toolbarTD_obj.size();i++)
				{
					if(toolbarTD_obj[i] == this)
					{
						// 0
						if(i == 0 && toolbarTD_obj.size() > 1)
						{
							e = e ? e : window.event;
							var mouse_x = e.clientX;
							var mouse_y = e.clientY;
							
							// img0
							var img0_centerx = parseFloat(findPosX(toolbarImg_obj[0].getDomObj()) + (parseFloat(toolbarImg_obj[0].getDomObj().style.width) / 2));
							var diff0 = Math.abs(mouse_x - img0_centerx);
							var addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
							if(addsize0 < 0)
							{
								addsize0 = 0;
							}
							
							// img1
							var img1_centerx = parseFloat(findPosX(toolbarImg_obj[1].getDomObj()) + (parseFloat(toolbarImg_obj[1].getDomObj().style.width) / 2));
							var diff1 = Math.abs(mouse_x - img1_centerx);
							var addsize1 = ((100 - (diff1*1.5)/3)*((max_width - default_width)/100));
							if(addsize1 < 0)
							{
								addsize1 = 0;
							}
							toolbarImg_obj[0].setSize(default_width+addsize0,default_width+addsize0);
							toolbarImg_obj[1].setSize(default_width+addsize1,default_width+addsize1);
						}
						else if(i == 0)
						{
							e = e ? e : window.event;
							var mouse_x = e.clientX;
							var mouse_y = e.clientY;
							// img0
							var img0_centerx = parseFloat(findPosX(toolbarImg_obj[0].getDomObj()) + (parseFloat(toolbarImg_obj[0].getDomObj().style.width) / 2));
							var diff0 = Math.abs(mouse_x - img0_centerx);
							var addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
							if(addsize0 < 0)
							{
								addsize0 = 0;
							}
							toolbarImg_obj[0].setSize(default_width+addsize0,default_width+addsize0);
						}
						
						// end
						else if(i == (toolbarTD_obj.size()-1))
						{
							e = e ? e : window.event;
							var mouse_x = e.clientX;
							var mouse_y = e.clientY;
							
							// end
							var img0_centerx = parseFloat(findPosX(toolbarImg_obj[i].getDomObj()) + (parseFloat(toolbarImg_obj[i].getDomObj().style.width) / 2));
							var diff0 = Math.abs(mouse_x - img0_centerx);
							var addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
							if(addsize0 < 0)
							{
								addsize0 = 0;
							}
							
							// end -1
							var img1_centerx = parseFloat(findPosX(toolbarImg_obj[i-1].getDomObj()) + (parseFloat(toolbarImg_obj[i-1].getDomObj().style.width) / 2));
							var diff1 = Math.abs(mouse_x - img1_centerx);
							var addsize1 = ((100 - (diff1*1.5)/3)*((max_width - default_width)/100));
							if(addsize1 < 0)
							{
								addsize1 = 0;
							}
							toolbarImg_obj[i].setSize(default_width+addsize0,default_width+addsize0);
							toolbarImg_obj[i-1].setSize(default_width+addsize1,default_width+addsize1);
						}
						
						// x
						else
						{
							e = e ? e : window.event;
							var mouse_x = e.clientX;
							var mouse_y = e.clientY;
							
							// i
							var img0_centerx = parseFloat(findPosX(toolbarImg_obj[i].getDomObj()) + (parseFloat(toolbarImg_obj[i].getDomObj().style.width) / 2));
							var diff0 = Math.abs(mouse_x - img0_centerx);
							var addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
							if(addsize0 < 0)
							{
								addsize0 = 0;
							}
							toolbarImg_obj[i].setSize(default_width+addsize0,default_width+addsize0);
							
							// i - 1
							if(i-1 >= 0)
							{
								img0_centerx = parseFloat(findPosX(toolbarImg_obj[i-1].getDomObj()) + (parseFloat(toolbarImg_obj[i-1].getDomObj().style.width) / 2));
								diff0 = Math.abs(mouse_x - img0_centerx);
								addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
								if(addsize0 < 0)
								{
									addsize0 = 0;
								}
								toolbarImg_obj[i-1].setSize(default_width+addsize0,default_width+addsize0);
							}
							
							// i + 1
							if(i+1 <= (toolbarTD_obj.size()-1))
							{
								img0_centerx = parseFloat(findPosX(toolbarImg_obj[i+1].getDomObj()) + (parseFloat(toolbarImg_obj[i+1].getDomObj().style.width) / 2));
								diff0 = Math.abs(mouse_x - img0_centerx);
								addsize0 = ((100 - (diff0*1.5)/3)*((max_width - default_width)/100));
								if(addsize0 < 0)
								{
									addsize0 = 0;
								}
								toolbarImg_obj[i+1].setSize(default_width+addsize0,default_width+addsize0);
							}
						}
						break;
					}
				}
			}
        }
        var tde	  = new Element('td',{'border':'0','align':'center','valign':'top','height':'150','width':'100'});
        tr.appendChild(tde);
        
        var image_default_width  = this.image_default_width;
        var image_default_height = this.image_default_height;
        //Add event when mouse out
		this.table.onmouseout = function(e) {
			e = e ? e : window.event;
			var mouse_x = mouseX(e);
			var mouse_y = mouseY(e);
			if(mouse_x > parseFloat(findPosX(this)) && mouse_x < (parseFloat(findPosX(this))+parseFloat(this.style.width))
			&& mouse_y > parseFloat(findPosY(this)) && mouse_y < (parseFloat(findPosY(this))+parseFloat(this.style.height)))
			{	
				return;
			}
			
			// resize to 75 * 75 (normal size)
			for(var i =0;i<toolbarImg_obj.size();i++)
			{
				toolbarImg_obj[i].setSize(image_default_width,image_default_height);
			}
		}
		tds.onmouseover = function(e) {
			e = e ? e : window.event;
			var mouse_x = e.clientX;
			var mouse_y = e.clientY;
			// resize to 75 * 75 (normal size)
			for(var i =0;i<toolbarImg_obj.size();i++)
			{
				toolbarImg_obj[i].setSize(image_default_width,image_default_height);
			}
		}
		
		tde.onmousemove = function(e) {
			e = e ? e : window.event;
			var mouse_x = e.clientX;
			var mouse_y = e.clientY;
			// resize to 75 * 75 (normal size)
			for(var i =0;i<toolbarImg_obj.size();i++)
			{
				toolbarImg_obj[i].setSize(image_default_width,image_default_height);
			}
		}
	}
});